package zxd.biz;

import redis.clients.jedis.Jedis;
import sun.misc.BASE64Encoder;
import zxd.entity.UserPage;
import zxd.util.Data;
import zxd.util.Util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * Created by 张旭东 on 2017/8/31.
 */
public class UserBL {
    //业务处理结果，成功失败的标识
    private boolean resultFlag = false;
    private final String key = "user:";
    private String errorMSG = "";
    private ArrayList resultList = new ArrayList();

    public String getErrorMSG() {
        return errorMSG;
    }

    public ArrayList getResultList() {
        return resultList;
    }

    Jedis jedis = new Jedis("127.0.0.1", 6379);
    private UserPage userPage;

    public boolean getResultFlag() {
        return resultFlag;
    }

    public boolean deal(String operate, Data data) {
        this.userPage = (UserPage) data.get("UserPage");
        if (operate.equals("insert")) {
            if (jedis.exists(key + userPage.getName())) {
                this.errorMSG = "该用户已经存在，请重新添加";
            } else {
                Map map = new HashMap();
                map.put("name",userPage.getName());
                map.put("pwd", this.getMD5Pwd(userPage.getPwd()));
                map.put("crtDate", Util.getCurrentDate());
                map.put("realName", userPage.getRealName());
                map.put("sex", userPage.getSex());
                jedis.hmset(key + userPage.getName(), map);
                this.resultFlag = true;
            }
            System.out.println("操作：Insert，数据：" + userPage);
        } else if (operate.equals("query")) {
            if (userPage != null) {
                System.out.println("userPage.getName()========" + userPage.getName());
                if (userPage.getName() != null) {
                    Map map = jedis.hgetAll(userPage.getName());
                    if (map.size() == 0) {
                        this.errorMSG = "该用户不存在，请重新查询";
                    } else {
                        this.resultList.add(map);
                        this.resultFlag = true;
                    }
                } else {
                    Set<String> set = jedis.keys("user*");
                    if (set.size() == 0) {
                        this.errorMSG = "Redis库中用户列表为空，请联系管理员";
                    } else {
//                        Map map;
                        for (String str : set) {
                            System.out.println(str);
                            Map map = jedis.hgetAll(str);
                            this.resultList.add(map);
                        }
                        this.resultFlag = true;
                    }
                }
            }
        }
        return true;
    }

    private String getMD5Pwd(String pwd) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            String newstr = base64en.encode(md5.digest(pwd.getBytes("utf-8")));
            return newstr;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
